// pages/order/comment/comment.js
import util from '../../../utils/utils';
import {
  CommentHttp
} from '../../../api/comment.js'

const commentHttp = new CommentHttp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderNumber: 0, // 订单号
    orderInfo: {}, // 订单详情
    storeInfo: {}, // 商店信息
    isNymCommentHorseman: true, // 是否匿名评价骑手
    isNymCommentStore: true, // 是否匿名评价店铺
    horsemanRate: 1, // 骑手评分: 1:满意, 0:不满意
    horsemanRateTags: [], // 骑手评分标签
    storeTotalRate: 5, // 店铺总评分
    storeQualityRate: 5, // 店铺品质评分
    storeServiceRate: 5, // 店铺服务评分
    storeCommentText: '', // 店铺评价文本
    storeCommentImg: [], // 店铺评价图片
    isCommentSuccess: false, // 是否评论成功
    isShowHorsemanRate: true, // 是否显示对骑手的评价
    icon: { // 骑手评价icon
      good: '/images/comment/good.png',
      bad: '/images/comment/bad.png'
    }
  },

  initPage: function(options) {
    // id --- 订单id
    // sourceType 订单来源类型 --- 1: 巷友, 2: 1688, 3: 积分兑换订单
    // receiveType 提货方式 --- 1: 门店自提, 2: 门店配送
    if (options.id) {
      let setDataObj = {}
      if (options.sourceType == 2 || options.receiveType == 1) {
        setDataObj.isShowHorsemanRate = false
      }
      setDataObj.orderNumber = options.id
      this.setData(setDataObj)
      this.initData()
    } else {
      util.msg("订单信息错误，请稍后重试！")
    }
  },

  // 初始化数据
  initData() {
    wx.showNavigationBarLoading();
    this.httpGetCommentStart();
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.initData()
  },

  // 匿名评价骑手
  onChangeNymCommentHorseman(event) {
    this.setData({
      isNymCommentHorseman: event.detail
    });
  },

  // 匿名评价店铺
  onChangeNymCommentStore(event) {
    this.setData({
      isNymCommentStore: event.detail
    });
  },

  // 评价骑手：满意/不满意
  onHorsemanRate(event) {
    const { type } = event.currentTarget.dataset
    this.setData({
      horsemanRate: type == 'bad' ? 0 : 1,
      horsemanRateTags: []
    });
  },

  // 选择评价骑手标签
  onSelectHorsemanTags(event) {
    const { horsemanRateTags } = this.data
    let tag = event.currentTarget.dataset.tag
    let index = horsemanRateTags.indexOf(tag)
    index === -1 ? horsemanRateTags.push(tag) : horsemanRateTags.splice(index, 1)
    this.setData({
      horsemanRateTags: horsemanRateTags
    })
  },

  // 评价店铺总评分
  onChangeStoreTotalRate(event) {
    this.setData({
      storeTotalRate: event.detail
    });
  },

  // 评价店品质评分
  onChangeStoreQualityRate(event) {
    this.setData({
      storeQualityRate: event.detail
    });
  },

  // 评价店铺服务评分
  onChangeStoreServiceRate(event) {
    this.setData({
      storeServiceRate: event.detail
    });
  },

  // 输入店铺评价文本
  onInputStoreCommentText(event) {
    this.setData({
      storeCommentText: event.detail.value
    });
  },

  // 上传平级店铺图片 - 文件读取完成后
  afterRead(event) {
    let list = this.data.storeCommentImg;
    list.push({
      url:event.detail.file.path
      })
    this.setData({
      storeCommentImg: list
    })
  },

  // 上传评价店铺图片 - 删除图片
  deleteRead(event) {
    let list = this.data.storeCommentImg;
    list.splice(event.detail.index, 1)
    this.setData({
      storeCommentImg: list
    })
  },

  // 提交评价
  onSubmit() {
    this.doSubmit()
  },

  formatData(res) {
    res['deliverFinishTime'] = util.formatTime('MM月dd日 HH:mm', res['deliverFinishTime']) + ' 左右送达';
    return res;
  },

  /**
   * 获取订单信息
   */
  httpGetCommentStart() {
    return commentHttp.getCommentStart({
      orderNumber: this.data.orderNumber
    }).then(res => {
      let info = this.formatData(res);
      this.setData({
        orderInfo: info,
        storeInfo: res.storeInfo
      });
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }).catch(err => {
      util.msg(err.msg && err.msg != '' ? err.msg : '网络错误,请检查后重试!')
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    })
  },

  httpAddComment(pics=[]) {
    let jso = {
      orderNumber: this.data.orderNumber,
      unionOrderId: this.data.orderNumber,
      anonymous: this.data.isNymCommentStore ? 1 : 0, // 商家评价是否匿名:0=不匿名 1=匿名
      deliverAnonymous: this.data.isNymCommentHorseman ? 1 : 0, // 骑手评价是否匿名:0=不匿名 1=匿名
      deliverScore: this.data.horsemanRate, // 对骑手是否满意: 0=不满意 1=满意
      deliverComment: this.data.horsemanRateTags.join(','), // 对骑手的评价
      storeScore: this.data.storeTotalRate, // 商家/菜品总打分
      tasteScore: this.data.storeQualityRate, // 服务打分
      packageScore: this.data.storeServiceRate, // 品质打分
      comment: this.data.storeCommentText, // 评价内容
      pictures: pics // 评价的图片地址list, json数组格式
    }
    return commentHttp.addComment(jso).then(res => {
      util.msg('评价成功!');
      this.setData({
        isCommentSuccess: true
      })
    }).catch(err => {
      util.msg(err.msg && err.msg != '' ? err.msg : '网络错误,请检查后重试!');
      // wx.hideNavigationBarLoading();
      // wx.stopPullDownRefresh();
    })
  },

  uploadImg(filePath) {
    return commentHttp.uploadImg(filePath)
  },

  doSubmit() {
    // 上传图片
    const uploadTasks = this.data.storeCommentImg.map((file) => this.uploadImg(file.url));
    Promise.all(uploadTasks).then(res => {
      console.log(res);
      let pics = []
      for (let i in res) {
        pics.push(res[i].url)
      }
      this.httpAddComment(pics);
    }).catch(err => {
      util.msg(err.msg && err.msg != '' ? err.msg : '网络错误,请检查后重试!');
    })
  },
})
